package com.duan.laugh.event.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duan.laugh.common.core.util.Assert;
import com.duan.laugh.common.core.util.Query;
import com.duan.laugh.common.core.util.R;
import com.duan.laugh.common.event.constants.EventConstant;
import com.duan.laugh.common.security.util.UserUtils;
import com.duan.laugh.event.ValidGroup.ValidGroup;
import com.duan.laugh.event.pojo.entity.EventModel;
import com.duan.laugh.event.pojo.vo.EventModelVO;
import com.duan.laugh.event.service.IEventModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 事件模型 controller
 * </p>
 *
 * @author liutf
 * @since 2019-11-12
 */
@Api(tags = "事件模型")
@RestController
@RequestMapping("/event-model")
public class EventModelController {
    @Autowired
    private IEventModelService eventModelService;

    /**
     * 新增事件模型
     *
     * @param eventModel 实体
     * @return true/false
     */
    @PostMapping
    @ApiOperation("新增事件模型")
    public R save(@RequestBody EventModel eventModel) {
        return R.ok(eventModelService.save(eventModel));
    }

    /**
     * 根据id查看事件模型
     *
     * @param id
     * @return EventModel
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查看事件模型")
    public R<EventModel> getById(@PathVariable String id) {
        return R.ok(eventModelService.getById(id));
    }

    /**
     * 根据id删除事件模型
     *
     * @param id
     * @return true/false
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除事件模型")
    public R removeById(@PathVariable String id) {
        return R.ok(eventModelService.removeById(id));
    }

    /**
     * 根据id修改事件模型
     *
     * @param eventModel 实体
     * @return true/false
     */
    @PutMapping
    @ApiOperation("根据id修改事件模型")
    public R updateById(@RequestBody EventModel eventModel) {
        return R.ok(eventModelService.updateById(eventModel));
    }

    @PostMapping("/all")
    @ApiOperation("0010106 新增事件模型")
    public R<Boolean> insertEventModel(@RequestBody @Validated({ValidGroup.Add.class}) EventModelVO eventModelVO) {
        eventModelVO.setStatus(EventConstant.ENABLE)
                .setDelFlag(EventConstant.ENABLE)
                .setCreateTime(LocalDateTime.now())
                .setTenantId(UserUtils.getUser().getTenantId());
        return R.ok(eventModelService.insert(eventModelVO));
    }

    @PutMapping("/all")
    @ApiOperation("0010107 根据id修改事件模型")
    public R<Boolean> updateEventModelById(@RequestBody @Validated({ValidGroup.Update.class}) EventModelVO eventModelVO) {
        eventModelVO.setUpdateTime(LocalDateTime.now());
        return R.ok(eventModelService.updateById(eventModelVO));
    }

    /*@DeleteMapping("/model")
    @ApiOperation("0010108 根据ids批量删除事件模型")
    public R<Boolean> deleteEventModelById() {
        return null;
    }*/

    @DeleteMapping("/all")
    @ApiOperation(value = "0010109 根据ids批量逻辑删除事件模型", notes = "参数：ids")
    public R<Boolean> deleteEventModelByIds(@RequestBody Map<String, Object> params) {
        String ids = getParamString(params, "ids");
        List<String> stringList = StrUtil.split(ids, ',', -1);
        return R.ok(eventModelService.removeByIds(stringList));
    }

    @PostMapping("/all/enabled")
    @ApiOperation(value = "0010110 根据ids批量启用事件模型", notes = "参数：ids")
    public R<Boolean> enableEventModelByIds(@RequestBody Map<String, Object> params) {
        String ids = getParamString(params, "ids");
        return R.ok(eventModelService.updateStatusEnableByIds(ids));
    }

    @PostMapping("/all/disabled")
    @ApiOperation(value = "0010111 根据ids批量禁用事件模型", notes = "参数：ids")
    public R<Boolean> disableEventModelByIds(@RequestBody Map<String, Object> params) {
        String ids = getParamString(params, "ids");
        return R.ok(eventModelService.updateStatusDisableByIds(ids));
    }

    @GetMapping("/all/{id}")
    @ApiOperation(value = "0010112 根据id查看事件模型", notes = "查看单条")
    public R<EventModelVO> selectEventModelVOById(@PathVariable Long id) {
        return R.ok(eventModelService.selectEventModelVOById(id));
    }

    /**
     * 0010114 分页查询本租户事件模型
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @GetMapping("/all/page")
    @ApiOperation("0010114 分页查询本租户事件模型")
    public IPage<EventModel> pageAll(@RequestParam Map<String, Object> params) {
        return eventModelService.page(new Query<>(params), new QueryWrapper<EventModel>().eq("tenant_id", UserUtils.getUser().getTenantId()));
    }

    /**
     * 获取map里的value
     *
     * @param params map
     * @param param  目标
     * @return string
     */
    private String getParamString(Map params, String param) {
        String value = String.valueOf(params.get(param));
        Assert.isBlank(value, param + "不能为空");
        return value;
    }
}

